<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../public/css/style.css">
</head>
<body>
    <h3>笔记列表页 
           <div>
           
           </div>
    </h3>
    
    <ul class="notelist">
       
    </ul>
    <script src="../public/js/ajax.js"></script>
    <script>
        let list = document.querySelector('.notelist');
        let menuBox = document.querySelector('h3 div');

        ajax.get('http://localhost/getlist',null,({data})=>{
            // console.log(data);

            let str = data.map(item=>`
                <li>
                    <h4>${item.title}</h4>
                    <p>
                        <a href="/updatenote">编辑</a>
                        <a href="javascript:;" onclick="removeNote('${item._id}')">删除</a>
                    </p>
                    <p class="time">${item.createtime}</p>
                </li>
            `).join('');

            list.innerHTML = str;

        })


        // 用sessionstorage判断
        let isLogin = sessionStorage.getItem('login');

        menuBox.innerHTML = isLogin?`<a href="/addnote">添加笔记</a><a href="/center">个人中心</a><a href="javascript:;" onclick="logout()">退出</a>`:`<a href="./register.html">注册</a><a href="./login.html">登录</a>`;


        function logout(){
            sessionStorage.removeItem('login');
            // window.location.href = './list.html'
            window.location.reload();
        }

        function removeNote(id){
            let key = sessionStorage.getItem('login');

            ajax.get(`http://localhost/delnote/${id}`,`key=${key}`,(data)=>{
                console.log(data);
            })
        }

        // if(isLogin){
        //     menuBox.innerHTML = `
        //         <a href="/addnote">添加笔记</a>
        //         <a href="/center">个人中心</a>
        //         <a href="/logout">退出</a>
        //     `;
        // }else{
        //     menuBox.innerHTML = `
        //         <a href="./register.html">注册</a>
        //         <a href="./login.html">登录</a>
        //     `;
        // }
    </script>
</body>
</html>